package com.github.ecbp.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.ecbp.common.constant.ConstantCode;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.user.service.entity.TMenu;
import com.github.ecbp.user.service.entity.TPermission;
import com.github.ecbp.user.service.entity.TPermissionMenuRelation;
import com.github.ecbp.user.service.mapper.PermissionMenuRelationMapper;
import com.github.ecbp.user.service.response.MenuRes;
import com.github.ecbp.user.service.response.PermissionRes;
import com.github.ecbp.user.service.service.PermissionMenuRelationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class PermissionMenuRelationServiceImpl extends BaseServiceImpl<PermissionMenuRelationMapper, TPermissionMenuRelation> implements PermissionMenuRelationService {

    @Override
    public List<PermissionRes> getPermissionVoByMenuId(Set<Long> menuIds) {
        QueryWrapper<TPermissionMenuRelation> permissionMenuRelationQueryWrapper = new QueryWrapper<>();
        permissionMenuRelationQueryWrapper.select("distinct p.id", "p.name", "p.title", "p.power", "p.description", "p.sort", "p" +
                ".system_type", "p.status", "pm.menu_id menuId");
        permissionMenuRelationQueryWrapper.in("pm.menu_id", menuIds);
        permissionMenuRelationQueryWrapper.eq("p.status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("p.delete_status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("pm.delete_status", ConstantCode.TRUE);
        return baseMapper.getPermissionVoByMenuId(permissionMenuRelationQueryWrapper);
    }

    @Override
    public List<MenuRes> getMenuVoByPermissionId(Set<Long> permissionIds) {
        QueryWrapper<TPermissionMenuRelation> permissionMenuRelationQueryWrapper = new QueryWrapper<>();
        permissionMenuRelationQueryWrapper.select("distinct m.id", "m.name", "m.parent_id", "m.title", "m.level", "m.sort", "m.icon", "m" +
                ".status", "m.status", "m.system_type"/*, "pm.permission_id"*/);
        permissionMenuRelationQueryWrapper.in("pm.permission_id", permissionIds);
        permissionMenuRelationQueryWrapper.eq("m.status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("m.delete_status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("pm.delete_status", ConstantCode.TRUE);
        final List<TMenu> menuByPermissionId = baseMapper.getMenuByPermissionId(permissionMenuRelationQueryWrapper);
        return TransformUtils.listCopy(menuByPermissionId, MenuRes.class);
    }

    @Override
    public List<TPermission> getPermissionByMenuId(Long menuId) {
        QueryWrapper<TPermissionMenuRelation> permissionMenuRelationQueryWrapper = new QueryWrapper<>();
        permissionMenuRelationQueryWrapper.select("distinct p.id", "p.name", "p.power");
        permissionMenuRelationQueryWrapper.eq("pm.menu_id", menuId);
        permissionMenuRelationQueryWrapper.eq("p.status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("p.delete_status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("pm.delete_status", ConstantCode.TRUE);
        return baseMapper.getPermissionByMenuId(permissionMenuRelationQueryWrapper);
    }

    @Override
    public List<TPermission> getPermissionByMenuId(Set<Long> menuIds) {
        QueryWrapper<TPermissionMenuRelation> permissionMenuRelationQueryWrapper = new QueryWrapper<>();
        permissionMenuRelationQueryWrapper.select("distinct p.id", "p.name", "p.power");
        permissionMenuRelationQueryWrapper.in("pm.menu_id", menuIds);
        permissionMenuRelationQueryWrapper.eq("p.status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("p.delete_status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("pm.delete_status", ConstantCode.TRUE);
        return baseMapper.getPermissionByMenuId(permissionMenuRelationQueryWrapper);
    }

    @Override
    public List<TMenu> getMenuByPermissionId(Long permissionId) {
        QueryWrapper<TPermissionMenuRelation> permissionMenuRelationQueryWrapper = new QueryWrapper<>();
        permissionMenuRelationQueryWrapper.select("distinct m.id", "m.name");
        permissionMenuRelationQueryWrapper.eq("pm.permission_id", permissionId);
        permissionMenuRelationQueryWrapper.eq("m.status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("m.delete_status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("pm.delete_status", ConstantCode.TRUE);
        return baseMapper.getMenuByPermissionId(permissionMenuRelationQueryWrapper);
    }

    @Override
    public List<TMenu> getMenuByPermissionId(Set<Long> permissionIds) {
        QueryWrapper<TPermissionMenuRelation> permissionMenuRelationQueryWrapper = new QueryWrapper<>();
        permissionMenuRelationQueryWrapper.select("distinct m.id", "m.name");
        permissionMenuRelationQueryWrapper.in("pm.permission_id", permissionIds);
        permissionMenuRelationQueryWrapper.eq("m.status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("m.delete_status", ConstantCode.TRUE);
        permissionMenuRelationQueryWrapper.eq("pm.delete_status", ConstantCode.TRUE);
        return baseMapper.getMenuByPermissionId(permissionMenuRelationQueryWrapper);
    }
}
